//
//  VisitorRigisterModel.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/23.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "VisitorRigisterModel.h"


@implementation VistorRigiterUseDataModel

@end

@implementation VisitorRigisterModel

+(void)visitorRegisterDidClickWithNeedData:(VistorRigiterUseDataModel *)data completion:(void (^)(BOOL))block
{
    NSString *type = @"";
    NSString *requesturl = @"";
    NSString *paramter = @"";
    if (data.way==VisitorRigisterUsePhone)
    {
        type = @"1";
        requesturl = @"phoneregister_2017_";
        paramter = [XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_phone=%@&asktype=%@&country=%@",data.phoneNumberOrEmail,type,data.areaCode]];
    }
    else if (data.way==VisitorRigisterUseEmail)
    {
        //邮箱找回密码获取验证码
        type = @"2";
        requesturl = @"mailforgetpwd";
        paramter = [XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_mail=%@",data.phoneNumberOrEmail]];
    }
    [DKHTTPRequest sendNewDataWithShortUrl:requesturl
                                  Param:paramter
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 VisitorRigisterModel *temp = [VisitorRigisterModel mj_objectWithKeyValues:serverDic];
                                 if (temp.code==200 || temp.code==101)
                                 {
                                     block(YES);
                                     [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"发送成功", nil)];
                                 }
                                 else
                                 {
                                     block(NO);
                                 }
                                 return YES;
                             } IsBuffering:NO
                         IsNeedReplaceWrap:NO
     IsUseUpLoadHead:NO];
}

+(void)submitRegistrationOrChangePassWord:(VistorRigiterUseDataModel *)rigister completion:(void (^)(BOOL))block
{
    NSString *type = @"";
    NSString *requesturl = @"";
    NSString *paramater = @"";
    if (rigister.way==VisitorRigisterUsePhone)
    {
        type = @"1";
        requesturl = @"phoneregister_setpasswd";
        paramater = [XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_phone=%@&validation=%@&passwd=%@&type=%@&country=%@&regtype=%@",rigister.phoneNumberOrEmail,rigister.verifyCode,rigister.password,type,rigister.areaCode,rigister.regtype]];
    }
    else if (rigister.way==VisitorRigisterUseEmail)
    {
        type = @"2";
        requesturl = @"resetpwd";
        paramater = [XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_mail=%@&validation=%@&passwd=%@&type=%@",rigister.phoneNumberOrEmail,rigister.verifyCode,rigister.password,type]];
    }
    [DKHTTPRequest sendDataWithShortUrl:requesturl
                                  Param:paramater
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 VisitorRigisterModel *temp = [VisitorRigisterModel mj_objectWithKeyValues:serverDic];
                                 if (temp.code==200)
                                 {
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.userid forKey:userIDString];
                                     if (rigister.way==VisitorRigisterUsePhone)
                                     {
                                         [[NSUserDefaults standardUserDefaults] setObject:rigister.password forKey:userPassword];
                                         [UserInfo shareUserInfo].userPassword = rigister.password;
                                     }
                                     [[NSUserDefaults standardUserDefaults]  synchronize];
                                     [UserInfo shareUserInfo].userID = temp.userid;
                                     
                                     block(YES);
                                 }
                                 else
                                 {
                                     block(NO);
                                 }
                                 return YES;
                             } IsBuffering:NO
                         IsNeedReplaceWrap:NO
     IsUseUpLoadHead:NO];
}

+(void)mailSubmitRegister:(VistorRigiterUseDataModel *)rigister completion:(void (^)(BOOL))block
{
    [DKHTTPRequest sendDataWithShortUrl:@"mailregister"
                                  Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_mail=%@&passwd=%@",rigister.phoneNumberOrEmail,rigister.password]]
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 
                                 VisitorRigisterModel *temp = [VisitorRigisterModel mj_objectWithKeyValues:serverDic];
                                 if (temp.code==200)
                                 {
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.userid forKey:userIDString];
                                     [[NSUserDefaults standardUserDefaults] setObject:rigister.password forKey:userPassword];
                                     [[NSUserDefaults standardUserDefaults]  synchronize];
                                     [UserInfo shareUserInfo].userID = temp.userid;
                                     [UserInfo shareUserInfo].userPassword = rigister.password;
                                     block(YES);
                                 }
                                 else
                                 {
                                     block(NO);
                                 }
                                 return YES;
                             } IsBuffering:NO
                         IsNeedReplaceWrap:NO
     IsUseUpLoadHead:NO];
}

+(void)vistorLogin:(VistorRigiterUseDataModel *)login completion:(void (^)(BOOL))block
{
    NSString *type = @"";
    if (login.way==VisitorRigisterUsePhone)
    {
        type = @"1";
        [DKHTTPRequest sendNewDataWithShortUrl:@"phonelogin_2017_"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_phone=%@&validation=%@&type=%@&country=%@",login.phoneNumberOrEmail,login.verifyCode,type,login.areaCode]]
                                 ImageArray:nil
                                    TimeOut:10
                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                     VisitorRigisterModel *temp = [VisitorRigisterModel modelWithDictionary:serverDic];
                                     if (temp.code==200)
                                     {
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.userid forKey:userIDString];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.password forKey:userPassword];
                                         [[NSUserDefaults standardUserDefaults] setBool:NO forKey:userIsExhibitor];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.username forKey:userName];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.cpname forKey:userCompany];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.head_img forKey:userHeadImgUrl];
                                         [[NSUserDefaults standardUserDefaults]  synchronize];
                                         [UserInfo shareUserInfo].userID = temp.userid;
                                         [UserInfo shareUserInfo].userPassword = temp.password;
                                         [UserInfo shareUserInfo].isExhibitor = NO;
                                         
                                         
                                         [UserInfo shareUserInfo].userName = temp.username;
                                         [UserInfo shareUserInfo].userCompany = temp.cpname;
                                         [UserInfo shareUserInfo].userHeadImgUrl = temp.head_img;
                                         
                                         
                                         block(YES);
                                     }
                                     else
                                     {
                                         block(NO);
                                     }
                                     return YES;
                                 } IsBuffering:NO
                          IsNeedReplaceWrap:YES
                            IsUseUpLoadHead:NO];

        
    }
    else if (login.way==VisitorRigisterUseEmail)
    {
        type = @"2";
        [DKHTTPRequest sendDataWithShortUrl:@"phonelogin"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_phone=%@&passwd=%@&type=%@&country=%@",login.phoneNumberOrEmail,login.password,type,login.areaCode]]
                                 ImageArray:nil
                                    TimeOut:10
                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                     VisitorRigisterModel *temp = [VisitorRigisterModel modelWithDictionary:serverDic];
                                     if (temp.code==200)
                                     {
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.userid forKey:userIDString];
                                         [[NSUserDefaults standardUserDefaults] setObject:login.password forKey:userPassword];
                                         [[NSUserDefaults standardUserDefaults] setBool:NO forKey:userIsExhibitor];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.username forKey:userName];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.cpname forKey:userCompany];
                                         [[NSUserDefaults standardUserDefaults] setObject:temp.head_img forKey:userHeadImgUrl];
                                         [[NSUserDefaults standardUserDefaults]  synchronize];
                                         [UserInfo shareUserInfo].userID = temp.userid;
                                         [UserInfo shareUserInfo].userPassword = login.password;
                                         [UserInfo shareUserInfo].isExhibitor = NO;
                                         
                                         
                                         [UserInfo shareUserInfo].userName = temp.username;
                                         [UserInfo shareUserInfo].userCompany = temp.cpname;
                                         [UserInfo shareUserInfo].userHeadImgUrl = temp.head_img;
                                         
                                         
                                         block(YES);
                                     }
                                     else
                                     {
                                         block(NO);
                                     }
                                     return YES;
                                 } IsBuffering:NO
                          IsNeedReplaceWrap:YES
                            IsUseUpLoadHead:NO];

    }
 }

+(void)exhibitorLogin:(VistorRigiterUseDataModel *)login completion:(void (^)(BOOL))block
{
    [DKHTTPRequest sendDataWithShortUrl:@"exhibitorlogin"
                                  Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"user_name=%@&passwd=%@",login.phoneNumberOrEmail,login.password]]
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 VisitorRigisterModel *temp = [VisitorRigisterModel modelWithDictionary:serverDic];
                                 if (temp.code==200)
                                 {
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.userid forKey:userIDString];
                                     [[NSUserDefaults standardUserDefaults] setObject:login.password forKey:userPassword];
                                     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:userIsExhibitor];
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.username forKey:userName];
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.cpname forKey:userCompany];
                                     [[NSUserDefaults standardUserDefaults] setObject:temp.head_img forKey:userHeadImgUrl];
                                     [[NSUserDefaults standardUserDefaults]  synchronize];
                                     [UserInfo shareUserInfo].userID = temp.userid;
                                     [UserInfo shareUserInfo].userPassword = login.password;
                                     [UserInfo shareUserInfo].isExhibitor = YES;
                                     
                                     
                                     [UserInfo shareUserInfo].userName = temp.username;
                                     [UserInfo shareUserInfo].userCompany = temp.cpname;
                                     [UserInfo shareUserInfo].userHeadImgUrl = temp.head_img;
                                     
                                     
                                     block(YES);
                                 }
                                 else
                                 {
                                     block(NO);
                                 }
                                 return YES;
                             } IsBuffering:NO
                      IsNeedReplaceWrap:YES
                        IsUseUpLoadHead:NO];
}

@end
